package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.order.SundayOrder;
import org.itboys.mobile.entity.mysql.order.SundayOrderProduct;

import org.itboys.mobile.service.mysql.web.SundayOrderService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_订单_后台_controller
 */
@Controller
@RequestMapping("/sunday/web/order")
public class SundayOrderController extends BaseController {
    @Autowired
    private SundayOrderService orderService;


    /**
     * 订单模块主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(
            HttpServletRequest request,
            Model model
            ){
        return "/sunday/order/index";
    }

    /**
     * 订单详情行订单商品信息
     * @param request
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/findOne")
    public String findOne(
            HttpServletRequest request,
            @RequestParam(value="id",required=true) Long id,
            Model model
            ){
          SundayOrder order = orderService.findOne(id);
          model.addAttribute("order",order);
        return"/sunday/order/input";
    }

    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                        HttpServletResponse response,
                       @RequestParam(value="orderStatus",required=true) Integer orderStatus
    ) {
        try {

            PageResult<SundayOrder> result = orderService.selectOrder(request,orderStatus);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/order/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }
    @RequestMapping("/product/select")
    public void product_select(HttpServletRequest request,
                       HttpServletResponse response,
                       @RequestParam(value="orderId",required=true) Long orderId
    ) {
        try {

            PageResult<SundayOrderProduct> result = orderService.selectOrderProduct(request,orderId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/order/product/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }

    }

    /**
     * 订单发货
     * @param request
     * @param response
     * @param id

     */
    @RequestMapping("/send")
    public void send(HttpServletRequest request,
                     HttpServletResponse response,
                            @RequestParam(value="id",required=true) Long id,
                            @RequestParam(value="sendCompany",required=false)String sendCompany,
                            @RequestParam(value="sendCompanyCode",required=false)String sendCompanyCode,
                            @RequestParam(value="sendCode",required=false) String sendCode
    ) {
        try {
            orderService.sendOrder(id,  sendCompany,sendCompanyCode,  sendCode);
            ResponseMessage.success();
        } catch (Exception e) {
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/kitchen/web/village/caigou/send------"+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE, CommonConstants.FAIL);
        }

    }

}
